package com.ziq.zcamp.post

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.amap.api.maps.model.LatLng
import com.gmlive.camp.data.exception.LocalException
import com.ziq.base.extension.launchWithTryAsynchronous
import com.ziq.zcamp.R
import com.ziq.zcamp.api.request.camp.CampListSearchParam
import com.ziq.zcamp.api.respond.camp.CampListRespond
import com.ziq.zcamp.api.service.CampService
import com.ziq.zcamp.api.service.NetWorkException
import com.ziq.zcamp.api.service.request
import com.ziq.zcamp.bean.place.CampListItem
import com.ziq.zcamp.bean.post.CampInfo
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.view.StatusView
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class SelectLocationCampFragmentViewModel @Inject constructor() : ViewModel() {

    @Inject
    lateinit var mCampService: CampService

    val statusShow = MutableLiveData<Int>()
    val listRequestFinished = MutableLiveData<Boolean>()
    val listHasMore = MutableLiveData<Boolean>()
    val list = MutableLiveData<MutableList<CampListItem>>()

    private var pageForData = 1
    private var pageSize = 20
    var keywordForReq: String? = null
    var myLatLng: LatLng? = null

    fun onRefresh(showProgress: Boolean = false){
        pageForData = 1
        if(showProgress){
            statusShow.postValue(StatusView.LOADING)
        }
        requestList()
    }

    fun onLoadMore(){
        requestList()
    }

    fun requestList(
        onSuccess: ((result: CampListRespond) -> Unit)? = {
            if (it.data != null){
                val tempList = mutableListOf<CampListItem>()
                if(pageForData != 1 && list.value != null){
                    tempList.addAll(list.value!!)
                }
                if(it.data?.list?.isNotEmpty() == true){
                    tempList.addAll(it.data!!.list!!)
                }
                list.postValue(tempList)

                pageForData++
                if((it.data?.total?:0) > tempList.size){
                    listHasMore.postValue(true)
                } else {
                    listHasMore.postValue(false)
                }

                if(tempList.isNotEmpty()){
                    statusShow.postValue(StatusView.IDLE)
                }else {
                    statusShow.postValue(StatusView.NO_DATA)
                }
            }else {
                if(pageForData == 1){
                    statusShow.postValue(StatusView.NET_ERROR)
                } else {
                    statusShow.postValue(StatusView.IDLE)
                }
                listHasMore.postValue(false)
            }
            listRequestFinished.postValue(true)
        },
        onFail: ((result: String) -> Unit)? = {
            if(pageForData == 1){
                statusShow.postValue(StatusView.NET_ERROR)
            } else {
                statusShow.postValue(StatusView.IDLE)
            }
            listRequestFinished.postValue(true)
        }) {


        launchWithTryAsynchronous(whenTry = {
            val respond = mCampService.campListSearch(CampListSearchParam().apply {
                page = pageForData
                page_size = pageSize
                keyword = keywordForReq
                latitude = "${myLatLng?.latitude}"
                longitude = "${myLatLng?.longitude}"
                this.list_sort_type = 1
            }).request()

            onSuccess?.invoke(respond)
        }, whenCatch = {
            withContext(Dispatchers.Main){
                when(it){
                    is LocalException -> {
                        onFail?.invoke(GlobalContextManager.getString(
                            R.string.all_net_error))
                    }
                    is NetWorkException -> {
                        onFail?.invoke(it.message?:"")
                    }
                    else -> {
                        onFail?.invoke(GlobalContextManager.getString(
                            R.string.all_net_error))
                    }
                }
            }
        })
    }

}